#!python3
# 文件名：client.py

import socket
import sys

ENCODING = 'utf-8'
BUFFER_SIZE = 1024

# 创建socket对象
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 连接服务器
clientSocket.connect(('localhost', 9999))

while True:
    request = input('>>').strip()
    if not request:
        break
    else:
        clientSocket.send(request.encode(ENCODING))  # 发送消息

    response = clientSocket.recv(BUFFER_SIZE)  # 读取消息,接收小于 BUFFER_SIZE 字节的数据
    if not response:
        break
    else:
        print(response.decode('utf-8'))

clientSocket.close()  # 关闭客户端
